package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// DeviceDailyReport 设备日报表数据
type DeviceDailyReport struct {
	ID         string  `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	MerchantID string  `json:"merchant_id" gorm:"column:merchant_id;type:string;size:40;uniqueIndex:report_uniq,priority:1;comment:商户id"`
	Name       string  `json:"name" gorm:"column:name;type:string;size:40;uniqueIndex:report_uniq,priority:2;comment:数据名称"`
	Value      float64 `json:"value" gorm:"column:value;type:float;size:64;comment:数值"`
	Date       string  `json:"date" gorm:"column:date;type:string;size:10;uniqueIndex:report_uniq,priority:3;comment:日期"`
	CreatedAt  int64   `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;index:created_at;comment:创建时间"`
}

// TableName 指定表名
func (DeviceDailyReport) TableName() string {
	return "device_daily_report"
}

// BeforeCreate 自增id
func (d *DeviceDailyReport) BeforeCreate(tx *gorm.DB) (err error) {

	if d.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	d.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &DeviceDailyReport{})
}
